package com.info.zhiduoduo.repository.mapper.evaluate;

import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.info.zhiduoduo.repository.dto.evaluate.EvaluateListDto;
import com.info.zhiduoduo.repository.entity.evaluate.Evaluate;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author cuichenglong
 * @since 2020-12-23
 */
public interface EvaluateMapper extends BaseMapper<Evaluate> {

    @Select(" <script> " +
            " SELECT eva.*, CASE WHEN evat.evaluate_id is null THEN '1' ELSE '2' END isFollowed," +
            " if(eva.evaluator_user_type=2," +
            "   (select business_manager from zdd_business_manager " +
            "   where business_manager_id= (select acc.business_manager_id from zdd_company_account acc where acc.compid=eva.evaluator_user_id)),'') as business_manager " +
            " FROM zdd_evaluate eva" +
            " left join " +
            "   (select evaluate_id FROM zdd_evaluate where " +
"                       IF(follow_date is null or follow_date='' ," +
"                       (platform_grade in (1,2) or whether_recommend in (2,4)) and (follow_explain is null or follow_explain = '') and createtime &lt;= #{workingDay}," +
"                       follow_date &lt;= NOW() )" +
            "    ) evat on eva.evaluate_id = evat.evaluate_id" +
            " WHERE eva.createtime BETWEEN #{startDate} AND #{endDate} " +
            " AND if(#{projectType}=0, 1=1, eva.project_type = #{projectType}) " +
            " and eva.evaluator_user_type in(${userTypes}) " +
            " <if test = \"platformGrade != null and platformGrade !=''\"> and eva.platform_grade in (${platformGrade}) </if> " +
            " <if test = \"appraiseeGrade != null and appraiseeGrade !=''\"> and eva.appraisee_grade in (${appraiseeGrade}) </if> " +
            " <if test = \"whetherRecommend != null and whetherRecommend !=''\"> and eva.whether_recommend in (${whetherRecommend}) </if> " +
            " and if(isnull(#{type}),1=1,if(#{type}=0,eva.follow_date='',eva.follow_date!='')) " +
            "  ORDER BY isFollowed desc, eva.createtime DESC " +
            " </script>")
    List<EvaluateListDto> queryEvaluateByCondition(@Param("projectType") int projectType, @Param("startDate") Date startDate, @Param("endDate") Date endDate, @Param("workingDay") Date workingDay,
                                                   @Param("userTypes") String userTypes, @Param("platformGrade") String platformGrade,
                                                   @Param("appraiseeGrade") String appraiseeGrade, @Param("whetherRecommend") String whetherRecommend,
                                                   @Param("type") Integer type);

    @Select("select count(1) FROM zdd_evaluate " +
            " where " +
            " IF(follow_date is null or follow_date='' ," +
            " (platform_grade in (1,2) or whether_recommend in (2,4)) and (follow_explain is null or follow_explain = '') and createtime <= #{workingDay}," +
            " follow_date <= NOW() )")
    Integer queryEvaluationCount(@Param("workingDay") Date workingDay);


    @Update(" update zdd_evaluate set follow_date=#{date} where evaluate_id=#{id} ")
    int updateEvaluateDate(@Param("id") Integer id,@Param("date") String date);
}
